import requests
import json
import re
import time


def translate(input_text):
    data_list = []
    url = "https://fanyi.baidu.com/ait/text/translate"

    headers = {
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36",
        "content-type": "application/json",
        "referer": "https://fanyi.baidu.com/mtpe-individual/multimodal?query=%E4%BD%A0%E5%A5%BD%0A%0A%0A&lang=zh2en&ext_channel=Aldtype",
        "origin": "https://fanyi.baidu.com",
    }

    dict_data = {
        "query": input_text,
        "from": "zh",
        "to": "en",
        "reference": "",
        "corpusIds": [],
        "needPhonetic": True,
        "domain": "common",
        "milliTimestamp": int(time.time() * 1000)
    }

    response_data = requests.post(url, data=json.dumps(dict_data), headers=headers)
    print('------------------------------------------------')
    detail_data = re.findall(('ata:\s(.*?)\n'), response_data.text, re.S)
    for d in detail_data:
        d_json = json.loads(d)
        print(d_json["data"])
        if d_json["data"]["event"] == 'GetDictSucceed':
            data_list.append(d_json["data"])

    return json.dumps(data_list, ensure_ascii=False, indent=4)


if __name__ == '__main__':
    input_text = input("请输入你要翻译的内容：")
    data_result = translate(input_text)
    print(data_result)
